
;*******************************
;4 Channel 303MHz Link.asm
;25-1-2015 
;*******************************


	list	p=12F629
	radix	dec
	include	"p12f629.inc"
	
		errorlevel	-302	; Dont complain about BANK 1 Registers during assembly

	__CONFIG	_MCLRE_OFF & _CP_OFF & _WDT_OFF & _INTRC_OSC_NOCLKOUT  ;Internal osc.

;_MCLRE_OFF  - master clear must be off for gp3 to work as input pin 

;****************************************************************
; variables - names and files
;****************************************************************

temp1		equ 20h	;
temp2		equ 21h	;
temp3		equ 22h	;
temp4		equ	23h	; 		
_flash		equ 26h ;for flashing the LED


;****************************************************************
;Equates
;****************************************************************
status		equ	0x03
rp1			equ	0x06
rp0			equ	0x05
GPIO 		equ 0x05
			

status		equ	03h
option_reg	equ 81h


		; bits on GPIO
				
pin7		equ	0	;GP0  Input from 303MHz module
pin6	 	equ	1	;GP1  Output A
pin5		equ	2	;GP2  Output B
pin4		equ	3	;GP3  not used
pin3		equ	4	;GP4  Output C
pin2		equ	5	;GP5  Output D   
 

		;bits
				
rp0			equ	5			;bit 5 of the status register

;****************************************************************
;Beginning of program
;****************************************************************
		org		0x00
		nop
		nop
		nop
		nop
		nop			
SetUp	bsf		status, rp0 	;Bank 1			
       	movlw	b'11001001'		;Set TRIS  GP1,2,4,5 out   GP0,input
		movwf	TRISIO	   	    ;		
		bcf		status, rp0		;bank 0
		movlw   07h         	;turn off Comparator ports
        movwf   CMCON       	;must be placed in bank 0  
        clrf    _flash     
		clrf 	GPIO       		;Clear GPIO of junk			
		goto 	Main	
			
;*********************
;* delays			*
;*********************


		;approx 16uS  delay

_10uS	goto 	$+1
        goto 	$+1
		goto 	$+1
        goto 	$+1
        goto 	$+1
        goto 	$+1
        goto 	$+1
		retlw 	00




_XuS	movlw	.100
		movwf	temp1
_2		nop
		decfsz 	temp1,f
		goto 	_2		
		retlw 	00
	
		
		
_1mS	movlw	.2
		movwf	temp2
_1		nop
		decfsz 	temp1,f
		goto 	_1
		decfsz 	temp2,f
		goto 	_1	
		retlw 	00	
		
		
_100mS	movlw	.100
		movwf	temp2
_100	nop
		decfsz 	temp1,f
		goto 	_100
		decfsz 	temp2,f
		goto 	_100	
		retlw 	00		
				
				
_500mS	movlw	0FFh
		movwf	temp2
_500	goto	$+1
		goto	$+1
		decfsz 	temp1,f
		goto 	_500
		decfsz 	temp2,f
		goto 	_500	
		retlw 	00	
		
		
			
;****************************
;* Sub Routines 			*
;****************************
		
LED    bsf  	gpio,1
       call	    _500mS
	   bcf  	gpio,1
       call	    _100mS	
       decfsz   _flash,1
       goto     LED
       call	    _500mS
       call	    _500mS
       call	    _500mS
       call	    _500mS 
	   goto     Main	
	   
	   
	   
Toggle	decfsz  _flash,1
        goto    $+4
        movlw 	02
		xorwf	gpio,1	;pin6 button A (1)
		goto    M2
		decfsz  _flash,1
        goto    $+4
        movlw 	02
		xorwf	gpio,1	;pin6 button A (2)
		goto    M2
		decfsz  _flash,1
        goto    $+4
        movlw 	04
		xorwf	gpio,1	;pin5 button B (3)
        goto    M2
		decfsz  _flash,1
        goto    $+4
        movlw 	04
		xorwf	gpio,1	;pin5 button B (4)
        goto    M2
        decfsz  _flash,1
        goto    $+4
        movlw 	04
		xorwf	gpio,1	;pin5 button B (5)
        goto    M2
        decfsz  _flash,1
        goto    $+4
        movlw 	10h
		xorwf	gpio,1	;pin3 button C (6)
		goto    M2
		decfsz  _flash,1
        goto    $+4
        movlw 	10h
		xorwf	gpio,1	;pin3 button C (7)
		goto    M2
        decfsz  _flash,1
        goto    $+4
        movlw 	10h
		xorwf	gpio,1	;pin3 button C (8)
		goto    M2
				
        decfsz  _flash,1
		goto    $+4
		movlw 	20h
		xorwf	gpio,1  ;pin2 button D (9)
        goto    M2	   
	    decfsz  _flash,1
        goto    $+4
        movlw 	20h
		xorwf	gpio,1  ;pin2 button D (10)
		goto    M2
	   	
	   	decfsz  _flash,1
        goto    M2	
        movlw 	20h
		xorwf	gpio,1  ;pin2 button D (11)
		goto    M2		
		
					
											
;****************************************************************
;* Main 							*
;****************************************************************

Main	bsf		status, rp0 	;Bank 1			
       	bcf 	TRISIO,0        ;make pin7 output		
		bcf		status, rp0		;bank 0

		bcf		gpio,0     		;make pin7 low to discharge 100n
        call	_1mS    		;discharge 100n
        
        bsf		status, rp0 	;Bank 1			
       	bsf 	TRISIO,0        ;make pin7 input		
		bcf		status, rp0		;bank 0	
		nop
		btfss  	gpio,0  
		goto    $-2
		bsf		status, rp0 	;Bank 1			
       	bcf 	TRISIO,0        ;make pin7 output		
		bcf		status, rp0		;bank 0

		bcf		gpio,0     		;make pin7 low to discharge 100n
        call	_1mS    		;discharge 100n
        clrf    _flash
        bsf		status, rp0 	;Bank 1			
       	bsf 	TRISIO,0        ;make pin7 input		
		bcf		status, rp0		;bank 0	
		
		btfsc  	gpio,0        
		goto    Toggle
		incf    _flash,1
		call    _XuS
		goto    $-4
		
		;switch debounce:
		
M2      bsf		status, rp0 	;Bank 1			
       	bcf 	TRISIO,0        ;make pin7 output		
		bcf		status, rp0		;bank 0

		bcf		gpio,0     		;make pin7 low to discharge 100n
        call	_1mS    		;discharge 100n
        
        bsf		status, rp0 	;Bank 1			
       	bsf 	TRISIO,0        ;make pin7 input		
		bcf		status, rp0		;bank 0
		call	_1mS 
		call	_1mS 	
		btfsc  	gpio,0  
		goto    M2
        goto    Main



									
		END
		
		